package com.example.a15787.weather_zyy_1235;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

class SearchedCityAdapter extends RecyclerView.Adapter<SearchedCityAdapter.ViewHolder> {
    private List<String> cities;

    public SearchedCityAdapter(List<String> cities) {
        this.cities = cities;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText(cities.get(position));
    }

    @Override
    public int getItemCount() {
        return cities.size();
    }

    public void moveItem(int fromPosition, int toPosition) {
        String item = cities.get(fromPosition);
        cities.remove(fromPosition);
        cities.add(toPosition, item);
        notifyItemMoved(fromPosition, toPosition);
    }

    public void removeItem(int position) {
        cities.remove(position);
        notifyItemRemoved(position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(android.R.id.text1);
        }
    }
}